﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Globalization;

namespace Pingy.Validators
{
    public class NumberValidator : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                string userString = value as string;
                if (string.IsNullOrEmpty(userString))
                    return new ValidationResult(false, "Timeout must be specified");

                for (int i = 0; i < userString.Length; i++)
                {
                    int number;
                    bool isNumber = int.TryParse(userString[i].ToString(), out number);
                    if (!isNumber)
                        return new ValidationResult(false, "Only numbers allowed");
                }
            }
            catch (Exception e)
            {
                return new ValidationResult(false, e.Message);
            }
            return ValidationResult.ValidResult;
        }
    }
}
